package com.sjtu.bookread;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import androidx.core.content.ContextCompat;
import com.sjtu.bookread.obj.LoginObj;
import com.sjtu.bookread.service.OperateData;
import com.sjtu.bookread.service.RecomService;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class RegisterActivity extends AppCompatActivity {

    private Button loginRegister;
    private EditText loginUser;
    private EditText loginPassword;
    private EditText loginPassword1;
    private LoginObj loginObj;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        changeStatusIconCollor(true);
        Window window =this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        window.setStatusBarColor(ContextCompat.getColor(this,R.color.colorGreyTitle));

        setContentView(R.layout.activity_register1);

        //绑定控件
        init();

        //注册按钮
        loginRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String user = loginUser.getText().toString();
                final String password = loginPassword.getText().toString();
                String password1 = loginPassword1.getText().toString();

                if (TextUtils.isEmpty(user)) {
                    //用户名为空
                    Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(password)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else if (TextUtils.isEmpty(password1)) {
                    Toast.makeText(RegisterActivity.this, "请确认密码",Toast.LENGTH_SHORT).show();
                } else if (!password.equals(password1)) {
                    Toast.makeText(RegisterActivity.this, "两次密码不一样，请验证", Toast.LENGTH_SHORT).show();
                }  else {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Looper.prepare();
                            loginObj = RecomService.getRecomService().getRegister(user,password);
                            if (loginObj != null && loginObj.getType().equals("0")) {
                                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                                //注册成功跳转到登录页面
                                startActivity( new Intent(RegisterActivity.this, LoginActivity.class));
                                RegisterActivity.this.finish();
                            } else
                            {
                                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
                            }
                            Looper.loop();
                        }
                    }).start();
                }

                //保存数据到SharedPreferences
                SharedPreferences.Editor editor = getSharedPreferences("register", MODE_PRIVATE).edit();
                editor.putString("User", user);
                editor.putString("password", password);
                editor.putString("password1", password1);
                editor.commit();

            }
        });
    }

    public void changeStatusIconCollor(boolean setDark) {
        View decorView = getWindow().getDecorView();
        if(decorView != null){
            int vis = decorView.getSystemUiVisibility();
            if(setDark){
                vis |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            } else{
                vis &= ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
            }
            decorView.setSystemUiVisibility(vis);
        }
    }

    /**
     * 绑定控件
     */
    private void init() {
        loginRegister = findViewById(R.id.L_register);
        loginUser = findViewById(R.id.L_user);
        loginPassword = findViewById(R.id.L_password);
        loginPassword1 = findViewById(R.id.L_password1);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (getCurrentFocus() != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(loginUser.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(loginPassword.getWindowToken(), 0);
            imm.hideSoftInputFromWindow(loginPassword1.getWindowToken(), 0);
        }
        return super.dispatchTouchEvent(ev);
    }
}
